﻿using System;
using System.Web.UI;
using Ext.Net;
using Ext.Net.Utilities;

//[assembly: WebResource("KamLib.resources.scripts.KamLib.utils.js", "application/x-javascript")]
//[assembly: WebResource("KamLib.resources.scripts.KamLib.Logic.js", "application/x-javascript")]

namespace KamLib.Controls
{
    public class MainPageBase : Page
    {
        public const string SameText = LogicPanel.SameText;
        public const string SameText2 = LogicPanel.SameText2;
        public const string SameText3 = LogicPanel.SameText3;
        public const string SameText4 = LogicPanel.SameText4;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            //var sb = new StringBuilder();
            //var a = typeof(MainPageBase).Assembly;
            //var rs = a.GetManifestResourceNames();
            //foreach (var wra in Attribute.GetCustomAttributes(a).OfType<WebResourceAttribute>())
            //{
            //    sb.AppendLine("Resource in the assembly: " + wra.WebResource +
            //        " with ContentType = " + wra.ContentType +
            //        " and PerformsSubstitution = " + wra.PerformSubstitution);
            //}

            //ClientScript.RegisterClientScriptResource(typeof(MainPageBase), "KamLib.resources.scripts.KamLib.utils.js");
            //ClientScript.RegisterClientScriptResource(typeof(MainPageBase), "KamLib.resources.scripts.KamLib.Logic.js");

            var dbuMsg = Application["dbUpdateMsg"] as string;
            if (dbuMsg != null)
            {
                Application.Remove("dbUpdateMsg");
                DbUpdater.ShowNotification(dbuMsg);
            }
        }

        protected GroupTab NewGroupTab(PageNode page)
        {
            var groupTab = new GroupTab { Expanded = !page.Collapsed, };

            groupTab.Listeners.TabChange.Handler = "Ext.fly(item.tabEl)[item.mainItem == tab ? 'removeClass' : 'addClass']('x-grouptabs-has-active-item');";

            var gpanel = new Panel
            {
                Title = page.TabTitle ?? page.Title,
                TabTip = page.TabTip,
                Cls = "dsh-items-view",
                PaddingSummary = "20px 40px 20px 40px",
                Layout = "Fit",

                ContentControls =
                {
                    new LiteralControl(
                        @"<div class=""dsh-item-wrap2d""><div class=""body-wrap""><h1 class=""page-header"">" + (page.PageTitle ?? page.Title) +
                        @"</h1><div class=""column3"">" + page.Desc + @"</div></div>"
                    ),
                    new Dashboard
                    {
                        ID = page.LID + "_Dsh" + page.Index, 
                        ButtonKind = DashboardButtonKind.Detail,
                        DataSource = page, 
                    },
                    new LiteralControl(@"</div>"),
                },
            };
            groupTab.Items.Add(gpanel);

            foreach (var p in page.Items)
            {
                if (p.Url.IsEmpty() || p.LID.IsEmpty()) continue;

                var lpanel = (UserControl)LoadControl(p.Url);
                lpanel.ID = p.LID;
                MainPagePanelEquiper.Apply(lpanel, p);

                var panel = new MainPagePanel
                {
                    PageNode = p,
                    Title = p.TabTitle ?? p.Title,
                    TabTip = p.TabTip ?? p.TabTitle ?? p.Title,
                    IconCls = "icon-" + p.Icon + "-24",
                    ContentControls = { lpanel },
                };

                groupTab.Items.Add(panel);
            }

            return groupTab;
        }

    }
}
